home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / uemlsrc.arc / word.c < prev    next >
C/C++ Source or Header  |  1987-08-24  |  18KB  |  589 lines

  1. /*
  2.  * The routines in this file
  3.  * implement commands that work word at
  4.  * a time. There are all sorts of word mode
  5.  * commands. If I do any sentence and/or paragraph
  6.  * mode commands, they are likely to be put in
  7.  * this file.  Added R.D.R Feb. 1986.
  8.  */
  9. #include        <stdio.h>
  10. #include        <ctype.h>
  11. #include        "ed.h"
  12.  
  13.  
  14. /* Word wrap or fill wrap depending on f flag value.
  15.  * Back-over whatever precedes the point on the current line and
  16.  * stop on the first word-break or the beginning of the line.
  17.  * If we reach the beginning of the line, jump back to the end of the
  18.  * word and start a new line.  Otherwise, break the line at the
  19.  * word-break, eat it, and jump back to the end of the word.
  20.  * Returns TRUE on success, FALSE on errors.
  21.  */
  22. wrapword(f, n)
  23. register int f, n;
  24. {
  25.         int oldp;
  26.         oldp = curwp->w_dotp;
  27.  
  28.         if (! backwword(NULL, 1)) /* punctuation marks */
  29.                 return(FALSE);
  30.         if (oldp != curwp->w_dotp && curwp->w_doto)
  31.                 {
  32.                 if (! backdel(NULL, 1))
  33.                         return(FALSE);
  34.                 if (! newline(TRUE, 1))
  35.                         return(FALSE);
  36.                 }
  37.         if(f)
  38.                 return(forwwword(NULL, 1));
  39.         return(forwwword(NULL, 1) && forwchar(NULL, 1) && backdel(NULL, 1));
  40. }
  41.  
  42. /* FILLPAR Meta command  Fill paragraph to specified fill column and indent
  43.  * column.  Bound to M-Q.
  44.  */
  45.  
  46. fillpar(f, n)
  47. register int f, n;
  48. {
  49.         register int s;
  50.         register short omo;
  51.         register LINE *omp;
  52.  
  53.         if (n > 1)
  54.                 setfillcol(f, n);
  55.  
  56.         if(fillcol == 0)
  57.                 {
  58.                 mlwrite("Fill column not set");
  59.                 (*term.t_beep)();
  60.                 return(FALSE);
  61.                 }
  62.         omp = curwp->w_markp;
  63.         omo = curwp->w_marko;
  64.         curwp->w_markp = curwp->w_dotp;
  65.         curwp->w_marko = curwp->w_doto;
  66.  
  67.         gotbop(FALSE, 1);
  68.         forwchar(FALSE, 1);
  69.         while((n = ltrw(FALSE, 1)) != EOF && n != NULL)
  70.                 forwline(NULL, 1);
  71.         gotbop(FALSE, 1);
  72.         forwchar(FALSE, 1);
  73.         while (TRUE)
  74.                 {
  75.                 if (llength(curwp->w_dotp) == NULL)
  76.                         break;
  77.                 if (curwp->w_dotp == curbp->b_linep)
  78.                         break;
  79.                 if (getccol(FALSE) > fillcol)
  80.                         {
  81.                         if (wrapword(TRUE, NULL) == FALSE)
  82.                                 break;
  83.                         continue;
  84.                         }
  85.                 if (curwp->w_doto ==  llength(curwp->w_dotp)
  86.                         && getccol(FALSE) <= fillcol)
  87.                         {
  88.                         if (forwchar(FALSE, 1) == FALSE)
  89.                                 break;
  90.                         if (curwp->w_dotp == curbp->b_linep)
  91.                                 break;  /* @ EOB */
  92.                         if (llength(curwp->w_dotp) == NULL)
  93.                                 break;  /* @ EOP */
  94.                         if (backchar(FALSE, 1) == FALSE)
  95.                                 break;
  96.                         if (clowsp(FALSE, NULL) == FALSE)
  97.                                 break;
  98.                         continue;
  99.                         }
  100.                 if (forwchar(FALSE, 1) == FALSE)
  101.                         break;
  102.                 }
  103.         curwp->w_dotp = curwp->w_markp;
  104.         curwp->w_doto = curwp->w_marko;
  105.         curwp->w_markp = omp;
  106.         curwp->w_marko = omo;
  107.         curwp->w_flag |= WFHARD;
  108.         curgoal = getccol(FALSE);
  109.         return(TRUE);
  110. }
  111.  
  112. /*
  113.  * PRIVATE VERSION OF INWORD() FOR WRAPWORD(), FORWWORD(), AND BACKWWORD()
  114.  * Return FALSE if the character at dot
  115.  * is a space character or above 0x7e.
  116.  * Otherwise return TRUE.  Any printing
  117.  * character below <DEL> that is not
  118.  * a space character may appear inside a
  119.  * word to be wrapped.  Using a special version
  120.  * of inword() allows us to keep the usual meaning
  121.  * of word for regular movement.
  122.  */
  123. static
  124. inwword()
  125. {
  126.         register int    c;
  127.  
  128.         if (curwp->w_doto == llength(curwp->w_dotp))
  129.                 return (FALSE);
  130.         c = lgetc(curwp->w_dotp, curwp->w_doto);
  131.         if (isspace(c) || c> '~')
  132.                 return (FALSE);
  133.         return (TRUE);
  134. }
  135.  
  136. /*
  137.  * PRIVATE VERSION OF BACKWORD() FOR WRAPWORD() AND FORWWWORD()
  138.  * Move the cursor backward by
  139.  * "n" words. All of the details of motion
  140.  * are performed by the "backchar" and "forwchar"
  141.  * routines. Error if you try to move beyond
  142.  * the buffers.
  143.  */
  144. static
  145. backwword(f, n)
  146. register int f, n;
  147. {
  148.         if (backchar(FALSE, 1) == FALSE)
  149.                 return (FALSE);
  150.         while (inwword() == FALSE)
  151.                 {
  152.                 if (backchar(FALSE, 1) == FALSE)
  153.                         return (FALSE);
  154.                 }
  155.         while (inwword() != FALSE)
  156.                 {
  157.                 if (backchar(FALSE, 1) == FALSE)
  158.                         return (FALSE);
  159.                 }
  160.         return (forwchar(FALSE, 1));
  161. }
  162.  
  163. /* PRIVATE VERSION OF FORWWORD() FOR WRAPWORD() AND BACKWWORD()
  164.  * Move the cursor forward by
  165.  * the specified number of words. All of the
  166.  * motion is done by "forwchar". Error if you
  167.  * try and move beyond the buffer's end.
  168.  */
  169. static
  170. forwwword(f, n)
  171. register int f, n;
  172. {
  173.         while (inwword() == FALSE)
  174.                 {
  175.                 if (forwchar(FALSE, 1) == FALSE)
  176.                         return (FALSE);
  177.                 }
  178.         while (inwword() != FALSE)
  179.                 {
  180.                 if (forwchar(FALSE, 1) == FALSE)
  181.                         return (FALSE);
  182.                 }
  183.         return (TRUE);
  184. }
  185.  
  186. /*
  187.  * Move the cursor forward by
  188.  * the specified number of words. All of the
  189.  * motion is done by "forwchar". Error if you
  190.  * try and move beyond the buffer's end.
  191.  */
  192. forwword(f, n)
  193. register int f, n;
  194. {
  195.         if (n < 0)
  196.                 return (backword(f, -n));
  197.         while (n--) {
  198.                 while (inword() == FALSE) {
  199.                         if (forwchar(FALSE, 1) == FALSE)
  200.                                 return (FALSE);
  201.                 }
  202.                 while (inword() != FALSE) {
  203.                         if (forwchar(FALSE, 1) == FALSE)
  204.                                 return (FALSE);
  205.                 }
  206.         }
  207.         return (TRUE);
  208. }
  209.  
  210. /*
  211.  * Move the cursor backward by
  212.  * "n" words. All of the details of motion
  213.  * are performed by the "backchar" and "forwchar"
  214.  * routines. Error if you try to move beyond
  215.  * the buffers.
  216.  */
  217. backword(f, n)
  218. register int f, n;
  219. {
  220.         if (n < 0)
  221.                 return (forwword(f, -n));
  222.         if (backchar(FALSE, 1) == FALSE)
  223.                 return (FALSE);
  224.         while (n--) {
  225.                 while (inword() == FALSE) {
  226.                         if (backchar(FALSE, 1) == FALSE)
  227.                                 return (FALSE);
  228.                 }
  229.                 while (inword() != FALSE) {
  230.                         if (backchar(FALSE, 1) == FALSE)
  231.                                 return (FALSE);
  232.                 }
  233.         }
  234.         return (forwchar(FALSE, 1));
  235. }
  236.  
  237. /*
  238.  * Move the cursor forward by
  239.  * the specified number of words. As you move,
  240.  * convert any characters to upper case. Error
  241.  * if you try and move beyond the end of the
  242.  * buffer. Bound to "M-U".
  243.  */
  244. upperword(f, n)
  245. register int f, n;
  246. {
  247.         register int    c;
  248.  
  249.         if (n < 0)
  250.                 return (FALSE);
  251.         while (n--) {
  252.                 while (inword() == FALSE) {
  253.                         if (forwchar(FALSE, 1) == FALSE)
  254.                                 return (FALSE);
  255.                 }
  256.                 while (inword() != FALSE) {
  257.                         c = lgetc(curwp->w_dotp, curwp->w_doto);
  258.                         if (islower(c)) {
  259.                                 c = toupper(c);
  260.                                 lputc(curwp->w_dotp, curwp->w_doto, c);
  261.                                 lchange(WFHARD);
  262.                         }
  263.                         if (forwchar(FALSE, 1) == FALSE)
  264.                                 return (FALSE);
  265.                 }
  266.         }
  267.         return (TRUE);
  268. }
  269.  
  270. /*
  271.  * Move the cursor forward by
  272.  * the specified number of words. As you move
  273.  * convert characters to lower ca